package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-02-09 18:15
 */
public class Q108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums == null) {
            return null;
        }
        return buildTree(nums, 0, nums.length - 1);
    }

    private TreeNode buildTree(int[] nums, int start, int end) {
        if(start > end) {
            return null;
        }

        TreeNode node = new TreeNode(nums[(start + end) / 2]);
        node.left = buildTree(nums, start, (start + end) / 2 - 1);
        node.right = buildTree(nums, (start + end) / 2 + 1, end);

        return node;
    }
}
